package com.zzk.lanouhn.myapplication1.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.ThreeSecondContentAdapter;
import com.zzk.lanouhn.myapplication1.modles.FullyLinearLayoutManager;
import com.zzk.lanouhn.myapplication1.modles.ThreeSifeSecond;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.CancelCollectDialog;
import com.zzk.lanouhn.myapplication1.view.CollectDialog;

import java.util.Timer;
import java.util.TimerTask;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * 二级页面-->小唐君无图页面
 * Created by lanouhn on 16/8/30.
 */
public class ThreeSifeSecondActivity extends Activity implements View.OnClickListener {

    private ImageButton back, like, ivshare,like_yellow;


    //文章，图，卖东西,评论
    private ThreeSifeSecond threeSifeSecond;
    private ThreeSecondContentAdapter threeSecondContentAdapter;
    private RecyclerView recyclerView;
    private CollectDialog collectDialog;
    private CancelCollectDialog cancelCollectDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_sife_second_three);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.ibback_act_sife_second_three);
        like = (ImageButton) findViewById(R.id.like_act_sife_second_three);
        ivshare = (ImageButton) findViewById(R.id.ivshare_act_sife_second_three);
        like_yellow= (ImageButton) findViewById(R.id.like_title_bar_like_yellow);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview_act_sife_second_essay_three);
    }

    private void initData() {
        Intent intent = getIntent();

        String item_ID=intent.getStringExtra("item_ID");
        //文章，图，卖东西,评论
        String urlSecond = "http://open4.bantangapp.com/topic/newInfo";
        String paramsSecond = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";

        try {
            HttpUtils.doPostAsyn(urlSecond, paramsSecond, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    threeSifeSecond = gson.fromJson(result, ThreeSifeSecond.class);

                    if (threeSifeSecond.getData().getPost_list().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }


                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    threeSecondContentAdapter = new ThreeSecondContentAdapter(ThreeSifeSecondActivity.this, threeSifeSecond);
                    recyclerView.setAdapter(threeSecondContentAdapter);
                    recyclerView.setLayoutManager(new FullyLinearLayoutManager(ThreeSifeSecondActivity.this));
                    break;
            }
        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        like.setOnClickListener(this);
        ivshare.setOnClickListener(this);
        like_yellow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回上一个页面
            case R.id.ibback_act_sife_second_three:
                finish();
                break;
            //第一次点击，收藏成功
            case R.id.like_act_sife_second_three:
//                like.setVisibility(View.INVISIBLE);
//                like_yellow.setVisibility(View.VISIBLE);
//                CollectDialog collectDialogSusseed=new CollectDialog(ThreeSifeSecondActivity.this)
//                        .showDialog(R.layout.dialog_collect_succeed,0,0);
                like_yellow.setVisibility(View.VISIBLE);
                like.setVisibility(View.INVISIBLE);
                collectDialog=new CollectDialog(ThreeSifeSecondActivity.this);
                Window window1=collectDialog.getWindow();
                window1.setWindowAnimations(R.style.dialogWindowAnim);
                WindowManager.LayoutParams lp1 = window1.getAttributes();
                window1.setGravity(Gravity.CENTER);
                lp1.alpha=1;
                lp1.width=320;
                lp1.height=420;
                window1.setAttributes(lp1);
                collectDialog.show();
                final Timer t1 = new Timer();
                t1.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        collectDialog.dismiss();
                        t1.cancel();
                    }
                },1500);
                break;
            //分享
            case R.id.ivshare_act_sife_second_three:
                showShare();
                break;
            //第二次点击，取消收藏
            case R.id.like_title_bar_like_yellow:
//                like.setVisibility(View.VISIBLE);
//                like_yellow.setVisibility(View.INVISIBLE);
//                CollectDialog collectDialogCalloff=new CollectDialog(ThreeSifeSecondActivity.this)
//                        .showDialog(R.layout.dialog_callof_collect,0,0);
                like.setVisibility(View.VISIBLE);
                like_yellow.setVisibility(View.INVISIBLE);
                cancelCollectDialog=new CancelCollectDialog(ThreeSifeSecondActivity.this);
                Window window2=cancelCollectDialog.getWindow();
                window2.setWindowAnimations(R.style.dialogWindowAnim);
                WindowManager.LayoutParams lp2 = window2.getAttributes();
                window2.setGravity(Gravity.CENTER);
                lp2.alpha=1;
                lp2.width=300;
                lp2.height=400;
                window2.setAttributes(lp2);
                cancelCollectDialog.show();
                final Timer t2 = new Timer();
                t2.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        cancelCollectDialog.dismiss();
                        t2.cancel();
                    }
                },1500);
                break;

        }
    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
}
